package com.lgx.mall.web.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.lgx.common.entity.Dictionary;
import com.lgx.common.web.Constants;
import com.lgx.common.web.util.DictUtils;
import com.lgx.mall.core.entity.Product;
import com.lgx.mall.core.service.IProductService;
import com.lgx.mall.core.vo.ProductVO;
import com.lgx.mall.view.service.IWebPageDetailService;
import com.lgx.right.entity.User;
import com.lgx.right.service.IUserService;

/**
 * 
 * 
 * @author ruyi
 *
 */
@Controller
@RequestMapping("/product")
public class ProductController {
	
	@Autowired
	private IWebPageDetailService webPageDetailService;
	
	@Autowired
	private IProductService productService;

	@Autowired
	private IUserService userService;

	public IUserService getUserService() {
		return userService;
	}

	public void setUserService(IUserService userService) {
		this.userService = userService;
	}

	public IWebPageDetailService getWebPageDetailService() {
		return webPageDetailService;
	}

	public IProductService getProductService() {
		return productService;
	}

	public void setWebPageDetailService(
			IWebPageDetailService webPageDetailService) {
		this.webPageDetailService = webPageDetailService;
	}

	public void setProductService(IProductService productService) {
		this.productService = productService;
	}

	/**
	 * 
	 * 
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value="/{productId}.htm",method=RequestMethod.GET)
	public String index(@PathVariable("productId") long productId,HttpServletRequest request,HttpServletResponse response){
		String path = request.getContextPath();
		String basePath = request.getScheme() + "://"
				+ request.getServerName() + ":" + request.getServerPort()
				+ path + "/";
		request.setAttribute("basePath", basePath);
		
		request.setAttribute("productId", productId);
		
		return "product/product";
	}
	
	@RequestMapping("/head.ftl")
	public String head(HttpServletRequest request,HttpServletResponse response){
		String path = request.getContextPath();
		String basePath = request.getScheme() + "://"
				+ request.getServerName() + ":" + request.getServerPort()
				+ path + "/";
		request.setAttribute("basePath", basePath);
		
		return "product/head";
	}
	/**
	 * 单个商品详细信息
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("/product.ftl")
	public String product(HttpServletRequest request,HttpServletResponse response){
		String path = request.getContextPath();
		String basePath = request.getScheme() + "://"
				+ request.getServerName() + ":" + request.getServerPort()
				+ path + "/";
		request.setAttribute("basePath", basePath);
		
		//单个商品详情信息
		String productId = request.getParameter("productId");
		Product product = this.productService.findProductById(Long.parseLong(productId));
		request.setAttribute("product", product);
		
		//获取区域
		Dictionary area = DictUtils.getDictByKindAndCode(DictUtils.PRODUCT_AREA, product.getAreaCategory()+"");
		request.setAttribute("area", area);
		//上周热兑换榜
		long categoryId = 133;
		List<ProductVO> details1 = this.webPageDetailService.findProductVOByAreaAndCid(null, categoryId);
		request.setAttribute("details1", details1);
		
		//相关品牌的产品
		List<Product> products = this.productService.findProductByIdAndBrand(product.getId(), product.getBrand().getId());
		request.setAttribute("products", products);
		
		//猜你喜欢的
		List<Product> guessproducts = this.productService.guessYouLike(5);
		request.setAttribute("guessproducts", guessproducts);

		return "product/product";
	}
	
	@RequestMapping("/login.htm")
	@ResponseBody
	public String login(HttpServletRequest request,HttpServletResponse response){
		String path = request.getContextPath();
		String basePath = request.getScheme() + "://"
				+ request.getServerName() + ":" + request.getServerPort()
				+ path + "/";
		request.setAttribute("basePath", basePath);
		
		String username = request.getParameter("username");
		String password = request.getParameter("password");
		String vaildcode = request.getParameter("vaildcode");
		
		User user = (User) request.getSession().getAttribute(Constants.LOGIN_USER);
		
		if((StringUtils.isEmpty(username) || StringUtils.isEmpty(password)))
		{
			return "0";
		}
		
		/* 验证码  */
		String _code = (String) request.getSession().getAttribute(Constants.VALIDATE_CODE);
		if(!vaildcode.toLowerCase().equals(_code.toLowerCase())){
			//request.setAttribute("errorMsgVaildcode", "验证码不正确！");
			return "1";
		}
		
		user = userService.loginUser(username, password);
		
		if(user == null){
			//request.setAttribute("errorMsgUser", "用户和密码不存在！");
			return "2";
		}
		
		request.getSession().setAttribute(Constants.LOGIN_USER, user);
		
		return "-1";
	}
	
	@RequestMapping("/pop-login.ftl")
	public String popLogin(HttpServletRequest request,HttpServletResponse response){
		String path = request.getContextPath();
		String basePath = request.getScheme() + "://"
				+ request.getServerName() + ":" + request.getServerPort()
				+ path + "/";
		request.setAttribute("basePath", basePath);
		
		return "product/pop-login";
	}
		
}
